package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.ContractDTO;
import com.ktjy.pojo.TbContract;
import com.ktjy.service.ITbContractService;
import com.ktjy.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 合同 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-28
 */
@RestController
@Slf4j
@Tag(name="合同管理")
@RequestMapping("/api/tb/contract")
public class TbContractController {
    @Autowired
    ITbContractService contractService;

    @Operation(summary = "查询合同列表")
    @GetMapping("/list")
    public AjaxResult list(ContractDTO contractDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        IPage<TbContract> page = contractService.findPage(contractDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @Operation(summary = "查询合同详情")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询用户详情
        try {
            TbContract contract=contractService.getById(id);
            if(contract!=null){
                return AjaxResult.success(contract);
            }
        } catch (Exception e) {
            log.error("TbContractController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @Operation(summary = "新增合同")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TbContract contract){
        try {
            contractService.saveContract(contract);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            log.error("TbContractController中save方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @Operation(summary = "修改合同")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody TbContract contract){
        try {
            contractService.updateContract(contract);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            log.error("TbContractController中update方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }
}
